package com.zbkj.front.controller;

import com.zbkj.dal.common.exception.CrmebException;
import com.zbkj.dal.pojo.request.PageParamRequest;
import com.zbkj.dal.pojo.request.ba.CreateDiscoveryRequest;
import com.zbkj.dal.pojo.response.ba.DiscoveryDetailResponse;
import com.zbkj.dal.pojo.response.ba.DiscoveryResponse;
import com.zbkj.dal.base.result.CommonResult;
import com.zbkj.service.service.DiscoveryService;
import com.zbkj.service.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 全球找货
 * @author tianzhonghai
 * 2023/10/9 08:42
 */
@Slf4j
@RestController
@RequestMapping("api/front/she/discovery")
@Api(tags = "全球找货")
public class SheDiscoveryController {
    @Autowired
    private DiscoveryService discoveryService;
    @Autowired
    private UserService userService;

    /**
     * 我的全球找货记录
     * @param status
     * @param pageParamRequest
     * @return
     */
    @ApiOperation(value = "我的全球找货记录")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<List<DiscoveryResponse>> listMyDiscovery(Integer status, PageParamRequest pageParamRequest) {
        return CommonResult.success(discoveryService.listMyDiscovery(status, pageParamRequest));
    }

    /**
     * 删除全球找货记录
     * @param id
     */
    @ApiOperation(value = "删除全球找货记录",notes = "删除全球找货记录")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public CommonResult<Boolean> deleteMyDiscovery(@PathVariable("id")Integer id) {
        return CommonResult.success(discoveryService.deleteMyDiscovery(id));
    }

    /**
     * 提交全球找货信息
     * @param createDiscoveryRequest
     * @return
     */
    @ApiOperation(value = "提交全球找货信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<Boolean> create(@Validated @RequestBody CreateDiscoveryRequest createDiscoveryRequest) {
        return CommonResult.success(discoveryService.create(createDiscoveryRequest));
    }

    /**
     * 全球找货记录详情
     * @param id
     * @return
     */
    @ApiOperation(value = "全球找货详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public CommonResult<DiscoveryDetailResponse> detail(@PathVariable("id") Integer id) {
        Integer userId = userService.getUserIdException();
        DiscoveryDetailResponse discoveryResponse = discoveryService.detail(id);
        if (!ObjectUtils.nullSafeEquals(discoveryResponse.getUid(), userId)){
            throw new CrmebException("您不能查看该信息");
        }
        return CommonResult.success(discoveryResponse);
    }
}
